# from CsvToData import CsvToData
from SettingConfig import SettingConfig

class Build :

    def  __init__(self) -> None:
        self.witers=dict()
        pass

    def build(self,_csvData):
        keys=_csvData.headerIndex.keys()
        models= SettingConfig.getModels(list(keys))
        for w in models:
            self.witers[w]=open(SettingConfig.buildFileName(w),'w',encoding='utf8')
            pass

        if _csvData.isValuType is True:
            #_defValueTypeInfo=["headPath","bg","img","icon","gif","frame"]
            index_headpath=[]
            index_bg=[]
            index_img=[]
            index_icon=[]
            index_gif=[]
            index_frame=[]
            types= _csvData.values[SettingConfig._defcsvInfo[0]].copy()
            #记录数据位置
            index=0
            for t in types:
                if t ==SettingConfig._defValueTypeInfo[0]:
                    index_headpath.append(index)
                    pass
                if t ==SettingConfig._defValueTypeInfo[1]:
                    index_bg.append(index)
                    pass
                if t ==SettingConfig._defValueTypeInfo[2]:
                    index_img.append(index)
                    pass
                if t ==SettingConfig._defValueTypeInfo[3]:
                    index_icon.append(index)
                    pass
                if t ==SettingConfig._defValueTypeInfo[4]:
                    index_gif.append(index)
                    pass
                if t ==SettingConfig._defValueTypeInfo[5]:
                    index_frame.append(index)
                    pass
                index=index+1
                pass
            index=0 
            #开始构建数据
            wsKeys=self.witers.keys()
            for w in wsKeys:
                curW = self.witers[w]
                curW.writelines(SettingConfig._buildBegin)
                for head in index_headpath:
                    str = SettingConfig.buildHeadInfo(_csvData.values[SettingConfig._defcsvInfo[1]][head],_csvData.values[w][head])
                    curW.writelines(str)
                    pass
                for i in index_bg:
                    str = SettingConfig.buildProperty(_csvData.values[SettingConfig._defcsvInfo[1]][i],_csvData.values[w][i],_csvData.values[SettingConfig._defcsvInfo[3]][i],isType=True,valueType=SettingConfig.buildSourceHead("bg"))
                    curW.writelines(str)
                    pass
                for i in index_img:
                    str = SettingConfig.buildProperty(_csvData.values[SettingConfig._defcsvInfo[1]][i],_csvData.values[w][i],_csvData.values[SettingConfig._defcsvInfo[3]][i],isType=True,valueType=SettingConfig.buildSourceHead("img"))
                    curW.writelines(str)
                    pass
                for i in index_icon:
                    str = SettingConfig.buildProperty(_csvData.values[SettingConfig._defcsvInfo[1]][i],_csvData.values[w][i],_csvData.values[SettingConfig._defcsvInfo[3]][i],isType=True,valueType=SettingConfig.buildSourceHead("icon"))
                    curW.writelines(str)
                    pass
                for i in index_gif:
                    str = SettingConfig.buildProperty(_csvData.values[SettingConfig._defcsvInfo[1]][i],_csvData.values[w][i],_csvData.values[SettingConfig._defcsvInfo[3]][i],isType=True,valueType=SettingConfig.buildSourceHead("gif"))
                    curW.writelines(str)
                    pass
                for i in index_frame:
                    str = SettingConfig.buildProperty(_csvData.values[SettingConfig._defcsvInfo[1]][i],_csvData.values[w][i],_csvData.values[SettingConfig._defcsvInfo[3]][i],isType=True,valueType=SettingConfig.buildSourceHead("frame"))
                    curW.writelines(str)
                    pass
                curW.writelines(SettingConfig._buildEnd)
                curW.close()
                pass
            print('数据构建完成')
            pass
        else:
            wsKeys=self.witers.keys()
            for w in wsKeys:
                curW = self.witers[w]
                curW.writelines(SettingConfig._buildBegin)
                i=0
                for d in _csvData.values[w]:
                    str = SettingConfig.buildProperty(_csvData.values[SettingConfig._defcsvInfo[1]][i] , d , _csvData.values[SettingConfig._defcsvInfo[3]][i])
                    curW.writelines(str)
                    i=i+1
                    pass
                curW.writelines(SettingConfig._buildEnd)
                curW.close()
                pass
            pass

        pass

    
    pass